package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeBillHouse;
import com.kehutong.charge.service.ChargeBillHouseService;
import com.kehutong.charge.util.enums.ChargeBillStatusEnum;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: kht
 * @description: 账单管理
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/bill/house")
public class ChargeBillHouseController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeBillHouseController.class);

    @Inject
    DeptService deptService;

    @Inject
    ChargeBillHouseService chargeBillHouseService;

    @ReqMapping("/page")
    @HasPermission("pay:charge:bill:mange:view")
    public Object pageHouse(JSONObject jsonObject){
        return chargeBillHouseService.findPage(jsonObject);
    }


    @ReqMapping("/page/export")
    @HasPermission("pay:charge:bill:mange:view")
    public void pageHouseExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject){
        chargeBillHouseService.pageHouseExport(req,resp,jsonObject);
    }


    @ReqMapping("/get")
    public Object getHouse(ChargeBillHouse entity) {

        return Result.success(entity);
    }

    @ReqMapping("/getObj")
    public Object getObj(JSONObject jsonObject) {

        return Result.success(chargeBillHouseService.getObj(jsonObject));
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:bill:mange:edit")
    @ReqMapping("/save")
    public Object save(ChargeBillHouse entity, Token token) throws Exception {
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());
            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setChargeName(token.getUsername());
        entity.setCreateName(token.getUsername());
        entity.setUpdateName(token.getUsername());
        return chargeBillHouseService.save(entity);
    }

    /**
     * 修改方法
     * @param entity
     * @return
     */
    @ReqMapping("/update")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object update(ChargeBillHouse entity, Token token) throws Exception {

        entity.setUpdateName(token.getUsername());
        return chargeBillHouseService.update(entity);
    }

    /**
     * 账单作废
     * @param entity
     * @return
     */
    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object setStatus(ChargeBillHouse entity, Token token) throws Exception {
        //修改账单作废
        entity.setBillStatus(ChargeBillStatusEnum.BILL_PAID_CLOSE);
        entity.setUpdateName(token.getUsername());
        chargeBillHouseService.update(entity);

        return Result.success();
    }

    /**
     * 生成缴费单
     * @param
     * @return
     */
    @ReqMapping("/generatePayBill")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object generatePaymentBill(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }

    /**
     * 批量导入
     * @param
     * @return
     */
    @ReqMapping("/batchImport")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object batchImport(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }

    /**
     * 批量导出
     * @param
     * @return
     */
    @ReqMapping("/batchExport")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object batchExport(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }

    /**
     * 收款账单查询
     * @param jsonObject
     * @return
     */
    @ReqMapping("/bills")
    //@HasPermission("pay:charge:bill:mange:view")
    public Object listBills(JSONObject jsonObject) {

        return chargeBillHouseService.findBillHouseList(jsonObject);
    }

    /**
     * 账单作废(单条或多条)
     * @param jsonObject
     * @return
     */
    @ReqMapping("/cancelHouseBill")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object cancelHouseBill(JSONObject jsonObject, Token token) {

        return chargeBillHouseService.cancelHouseBill(jsonObject,token);
    }

}
